//
//  LoginViewController.m
//  HeartWorldClient
//
//  Created by Mac on 2017/7/13.
//  Copyright © 2017年 谭明权. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "CheckVerifiCodeViewController.h"
#import "YCLoginManager.h"
//#import "YCProfileViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
   
    [_phoneTextField becomeFirstResponder];
    [_phoneTextField addTarget:self action:@selector(validationChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _nextBtn.layer.cornerRadius = 6;
    _nextBtn.clipsToBounds = YES;
    
    [self configNavgationBar];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.phoneTextField.text = [[YCLoginManager shareManager].phoneNumber telephoneNumberFormat];
}

- (void)configNavgationBar {
    UIImage *img = [UIImage imageNamed:@"叉"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIButton *navLeftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 40, 30)];
    navLeftBtn.imageEdgeInsets= UIEdgeInsetsMake(0, -30, 0, 0);
    [navLeftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [navLeftBtn setImage:img forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navLeftBtn];
    
    UIButton *navRightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 40, 30)];
    [navRightBtn setTitle:@"注册" forState:UIControlStateNormal];
    [navRightBtn setTitleColor:[UIColor colorWithCommonType:commonColorBlue] forState:UIControlStateNormal];
    navRightBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [navRightBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navRightBtn];
}

- (IBAction)nextBtnClick:(id)sender {
    
   [self checkAndGotoLoginVc];
}

- (void)registerBtnClick:(id)sender {
    
    RegisterViewController *registerVc = [[RegisterViewController alloc]init];
    
    [self.navigationController pushViewController:registerVc animated:YES];
    
}

/*
- (void)backClick {
    UIViewController *toVC = nil;
    for (UIViewController *vc in self.navigationController.viewControllers) {
        
        if ([vc isKindOfClass:[YCProfileViewController class]]) {
            toVC = vc;
            break;
        }
    }
    if (toVC) {
        [self.navigationController popToViewController:toVC animated:YES];
    } else {
        [super backClick];
    }
    //退出登录页面时，把phoneNumber重置
    [YCLoginManager shareManager].phoneNumber = [YCUserManager sharedInstance].memberMobile;
}
*/

/**
 *  当输入手机号码变化
 *
 *  @param textfied
 */
#pragma mark - 手机号码输入11位时自动跳到密码输入框
-(void)validationChanged:(UITextField *)textfied {
    textfied.text = [textfied.text telephoneNumberFormat];
    if (textfied.text.length >= 13) {
        
        textfied.text = [textfied.text substringWithRange:NSMakeRange(0, 13)];
        
        [self.view endEditing:YES];
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length + string.length > 13) {
        return NO;
    }
    
    return YES;
}

- (void)checkAndGotoLoginVc {
    
     [self.view endEditing:YES];
    
    if (![NSString yc_isValidPhone:[_phoneTextField.text cleanTelephoneNumber]]) {
        
        [self presentFailureTips:@"手机号格式不对"];
        
        return ;
    }
    
    CheckVerifiCodeViewController *vc = [[CheckVerifiCodeViewController alloc] init];
    
    vc.phoneStr = _phoneTextField.text;
    
   
    vc.isGetCodeAgain = YES;

    
    vc.typeFrom = TypeFromLogin;
    
    [self.navigationController pushViewController:vc animated:YES];
    
    [YCLoginManager shareManager].phoneNumber = [_phoneTextField.text cleanTelephoneNumber];
    [YCUserTool sharedTools].linkMobile = [_phoneTextField.text cleanTelephoneNumber];


    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}



@end
